package controllers;

import java.util.List;

import model.Player;
import model.Room;
import model.ServerResponse;
import play.mvc.*;
import static play.libs.Json.toJson;

public class ControllerFassade extends Controller {

  public static Result index() {
	  return ok("Teste");
  }
  public static Result createAcc(String login, String name, String passwd, String email) {
	  PlayerRegistry pr = PlayerRegistry.getInstance();
	  Player p = pr.createPlayer(name, login, passwd, email);
	  ServerResponse sr;
	  if (p!=null) {
		   sr = ServerResponseCreator.buildResponse(true, 1, p); 
	  }
	  else {
		  sr = ServerResponseCreator.buildResponse(false, 1);
	  }
	  return ok(toJson(sr));
  }
  
  public static Result login(String login, String passwd) {
	  PlayerRegistry pr = PlayerRegistry.getInstance();
	  Player p = pr.login(login, passwd);
	  ServerResponse sr;
	  if (p!=null) {
		   sr = ServerResponseCreator.buildResponse(true, 1, p); 
	  }
	  else {
		  sr = ServerResponseCreator.buildResponse(false, 1);
	  }
	  return ok(toJson(sr));
  }
  
  public static Result newRoom(String requestPlayer, String name, String passwd) {
	  RoomRegistry rr = RoomRegistry.getInstance();
	  PlayerRegistry pr = PlayerRegistry.getInstance();
	  Player requester = pr.findPlayer(requestPlayer);
	  
	  ServerResponse sr = null;
	  if (requester==null) {
		  sr = ServerResponseCreator.buildResponse(false, 1);
	  } else {
		  	Room r = rr.newRoom(requester, name, passwd);
		  	if (r != null) {
		  		sr = ServerResponseCreator.buildResponse(true, 2, r);
		  	} else {
		  		sr = ServerResponseCreator.buildResponse(false, 2);
		  	}
	  }
	  return ok(toJson(sr));
  }
  
  public static Result joinRoom(String requestPlayer, String roomName, String passwd) {
	  PlayerRegistry pr = PlayerRegistry.getInstance();
	  Player p = pr.findPlayer(requestPlayer);
	  RoomRegistry rr = RoomRegistry.getInstance();
	  ServerResponse sr = null;
	  if (p==null) {
		  sr = ServerResponseCreator.buildResponse(false, 1); 
	  } else {
		  Room r = rr.findRoom(roomName);
		  if (r!=null && r.getPasswd().compareTo(passwd)==0) {
			  r.addPlayer(p);
			  if (p.save()) {
				  sr = ServerResponseCreator.buildResponse(true, 2, r);
			  } else {
				  sr = ServerResponseCreator.buildResponse(false, 2);
			  }
		  } else {
			  sr = ServerResponseCreator.buildResponse(false, 2);
		  }
	  }
	  return ok(toJson(sr));
  }
  
  public static Result leaveRoom(String requestPlayer, String roomName) {
	  PlayerRegistry pr = PlayerRegistry.getInstance();
	  RoomRegistry rr = RoomRegistry.getInstance();
	  
	  Player p = pr.findPlayer(requestPlayer);
	  ServerResponse sr = null;
	  if (p==null) {
		  sr = ServerResponseCreator.buildResponse(false, 1); 
	  } else {
		  Room r = rr.findRoom(roomName);
		  if (r!=null) {
			  r.removePlayer(p);
			  if (r.save()) {
				  sr = ServerResponseCreator.buildResponse(true, 2, r);
			  } else {
				  sr = ServerResponseCreator.buildResponse(false, 2);
			  }
		  } else {
			  sr = ServerResponseCreator.buildResponse(false, 2);
		  }
	  }
	  return ok(toJson(sr));
  }
  
  public static Result findRoom(String requestPlayer, String roomName) {
	  RoomRegistry rr = RoomRegistry.getInstance();
	  Room r = rr.findRoom(roomName);
	  ServerResponse sr = null;
	  if (r!=null) {
		  sr = ServerResponseCreator.buildResponse(true, 2, r);
	  } else {
		  sr = ServerResponseCreator.buildResponse(false, 2);
	  }
	  return ok(toJson(sr));
  }

  public static Result findRooms(String requestPlayer) { // todas as salas
	  RoomRegistry rr = RoomRegistry.getInstance();
	  List<Room> rooms = rr.getRooms();
	  ServerResponse sr = ServerResponseCreator.buildResponse(true, 3, rooms);
	  return ok(toJson(sr));
  }
  
  public static Result updateGame(String requestPlayer, long x, long y, long z, String requestRoom) {
	  RoomRegistry rr = RoomRegistry.getInstance();
	  PlayerRegistry pr = PlayerRegistry.getInstance();
	  
	  Room r = rr.findRoom(requestRoom);
	  Player p = pr.findPlayer(requestPlayer);
	  
	  ServerResponse sr = null;
	  if (r!=null && p != null && p.getLogin().compareTo(r.getBombPlayer().getLogin())==0) {
		  r.getBombPlayer().setPosition(x,y,z);
		  if (r.save()) {
			  sr = ServerResponseCreator.buildResponse(true, 1, r);
		  } else {
			  sr = ServerResponseCreator.buildResponse(false, 1);
		  }
	  }
	  else sr = ServerResponseCreator.buildResponse(true, 1, p);
	  return ok(toJson(sr));
  }
  
  public static Result getBombPosition(String requestPlayer, String roomName) {
	  RoomRegistry rr = RoomRegistry.getInstance();
	  PlayerRegistry pr = PlayerRegistry.getInstance();
	  
	  Room r = rr.findRoom(roomName);
	  Player p = pr.findPlayer(requestPlayer);
	  
	  ServerResponse sr = null;
	  if (r!=null && p!=null) {
		 if (r.playerIsIn(p)) {
			 String pos = r.getBombPlayer().getX()+","+ r.getBombPlayer().getY() +","+r.getBombPlayer().getZ();
			 sr = ServerResponseCreator.buildResponse(true, 5, pos);
		 } else {
			 sr = ServerResponseCreator.buildResponse(false, 5);
		 }
	  }
	  else sr = ServerResponseCreator.buildResponse(false, 5);
	  return ok(toJson(sr));
  }
}